package net.xju3_4.controller;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.MerchantEntity;
import net.xju3_4.service.AdminMerchantService;
import net.xju3_4.service.impl.AdminMerchantServiceImpl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

//使用post方法进行
@RequestMapping(("/api/admin/merchant"))
public class AdminMerchantController {
   private AdminMerchantService adminMerchantService = new AdminMerchantServiceImpl();
@RequestMapping("/pagelist.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
     req.setCharacterEncoding("utf-8");
     String pageNoStr = req.getParameter("pageNo");
     String pageSizeStr = req.getParameter("pageSize");
     Integer pageNo = pageNoStr == null || pageSizeStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
     Integer pageSize =pageNoStr==null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);
     String username=req.getParameter("username");
     String merchantName=req.getParameter("merchantName");
     String address=req.getParameter("address");
    MerchantEntity merchant = new MerchantEntity();
    if(username != null) merchant.setUsername(username);
    if(merchantName != null) merchant.setMerchantName(merchantName);
    if(address!=null) merchant.setAddress( address);
    // 创建服务实例

    PageInfo<MerchantEntity> pageInfo = adminMerchantService.pagelist(pageNo, pageSize, merchant);
    if(pageInfo != null)
        JSONUtil.printByJSON(resp, Result.success(pageInfo,"获取成功"));
    else
        JSONUtil.printByJSON(resp, Result.fail("获取失败"));
    }
    @RequestMapping("/forbid.do")
    public void forbid(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    req.setCharacterEncoding("utf-8");
    String idStr = req.getParameter("id");
    Integer id = Integer.parseInt(idStr);
    if(adminMerchantService.forbid(id)>0)
        JSONUtil.printByJSON(resp,Result.success("禁用成功"));
    else
        JSONUtil.printByJSON(resp,Result.fail("禁用失败"));
    }
    @RequestMapping("/activate.do")
    public void activate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     req.setCharacterEncoding("utf-8");
     String idStr = req.getParameter("id");
     Integer id = Integer.parseInt(idStr);
     if(adminMerchantService.activate(id)>0)
        JSONUtil.printByJSON(resp,Result.success("激活成功"));
    else
        JSONUtil.printByJSON(resp,Result.fail("激活失败"));
    }
}

